import { Injectable } from '@nestjs/common'
import { CreateRewardDto } from './dto/create-reward.dto'
import { UpdateRewardDto } from './dto/update-reward.dto'
import { PrismaService } from '../prisma/prisma.service'

@Injectable()
export class RewardService {
    constructor(private readonly prisma: PrismaService) { }

    create(createRewardDto: CreateRewardDto) {
        return this.prisma.reward.create({
            data: createRewardDto
        })
    }

    async findAll(query: { pageIndex: number, pageSize: number }) {
        const list = await this.prisma.reward.findMany({
            skip: (query.pageIndex - 1) * query.pageSize,
            take: query.pageSize,
            orderBy: { createTime: 'desc' }
        })
        const total = await this.prisma.reward.count()
        return { list, total }
    }

    findOne(id: number) {
        return this.prisma.reward.findUnique({
            where: { id }
        })
    }

    update(id: number, updateRewardDto: UpdateRewardDto) {
        return this.prisma.reward.update({
            where: { id },
            data: updateRewardDto
        })
    }

    remove(id: number) {
        return this.prisma.reward.delete({
            where: { id }
        })
    }
}
